package com.ycsj.goldmedalnewconcept.master.activity;

import android.support.v4.app.Fragment;
import android.view.KeyEvent;

import com.ycsj.common.appupdate.AppUpdateUtil;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.toast.ToastUtils;
import com.ycsj.common.view.BottomBar;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.master.fragment.MasterAdmissionsHelperFragment;
import com.ycsj.goldmedalnewconcept.master.fragment.MasterManagerFragment;
import com.ycsj.goldmedalnewconcept.master.fragment.MasterMeFragment;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

/**
 * 校长端主界面
 */
public class MasterMainActivity extends BaseActivity {

    @BindView(R.id.bottombar)
    BottomBar bottombar;

    //底部导航栏的文字集合
    private String[] tabTexts = {"", "", ""};

    //选中的图标集合
    private int[] selectImgs = {R.drawable.master_gl_select, R.drawable.master_help_select, R.drawable.master_me_select};

    //未选中的图标集合
    private int[] normalImgs = {R.drawable.master_gl_normal, R.drawable.master_help_normal, R.drawable.master_me_normal};

    //当前需要显示的Framgent
    private Fragment mShowFragment;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_master_main;
    }

    @Override
    protected void init() {

        //沉浸状态栏
        initStateBar();

        //设置初始状态
        initState();

        //版本更新
        AppUpdateUtil.updateVersion(this);

        //初次进入显示主页的Fragment
        initHomeFramgent();
    }

    /**
     * 初始化状态的方法
     */
    private void initState() {
        bottombar.setData(selectImgs, normalImgs, tabTexts);
        //设置选中监听事件
        bottombar.setOnSelectedListener(new BottomBar.onTabSelectedListener() {
            @Override
            public void onSelected(int pos) {
                if (pos == 0) {
                    switchPage(MasterManagerFragment.TAG, MasterManagerFragment.class);
                } else if (pos == 1) {
                    switchPage(MasterAdmissionsHelperFragment.TAG, MasterAdmissionsHelperFragment.class);
                } else if (pos == 2) {
                    //切换我的界面时候再次设置缓存大小
                    BusInfo busInfo = new BusInfo();
                    busInfo.type = MasterMeFragment.UPDATECACHESIZE;
                    EventBus.getDefault().post(busInfo);
                    switchPage(MasterMeFragment.TAG, MasterMeFragment.class);
                }
            }
        });
    }

    /**
     * 初次进入的时候显示主页Framgent
     */
    private void initHomeFramgent() {

        //初次进入,将首页的Fragment显示出来

        mShowFragment = MasterManagerFragment.newInstance();

        switchPage(MasterManagerFragment.TAG, MasterManagerFragment.class);

    }

    /**
     * @param tag 标记名
     * @param cls 需要创建的Fragment的类名
     */
    private void switchPage(String tag, Class cls) {
        // 通过此方法我们可以查找在当前容器中是否存在该tag名的Fragment
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
        if (fragment == null) {
            try {
                // 通过反射创建出该类对象
                Fragment instance = (Fragment) cls.newInstance();
                getSupportFragmentManager().beginTransaction()
                        .hide(mShowFragment)
                        .add(R.id.fl_contain, instance, tag)
                        .commit();
                mShowFragment = instance;
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } else {
            getSupportFragmentManager().beginTransaction().hide(mShowFragment) // 设置Fragment的切换动画
                    .show(fragment).commit();
            mShowFragment = fragment;
        }
    }

    private boolean needAlarm = false;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        //判断程序进入后台是否是用户自身造成的（触摸返回键或HOME键），是则无需弹出警示。
        if ((keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) && event.getRepeatCount() == 0) {
            needAlarm = false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        //若程序进入后台不是用户自身造成的，则需要弹出警示
        if (needAlarm) {
            //弹出警示信息
            ToastUtils.show("APP异常运行到后台");
        }

        super.onPause();
    }
}
